home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / readers / skim-0.8 / skim-0 / skim-0.8.4 / VarBufTest.c < prev    next >
C/C++ Source or Header  |  1996-02-18  |  11KB  |  424 lines

  1. /*
  2.  * NAME
  3.  *   VarBufTest.c
  4.  * DESCRIPTION
  5.  *   Test the VarBuf ADT. Run this test by typing 'make test' in the source
  6.  *   directory of skim.
  7.  * COPYRIGHT
  8.  *   VarBuf - Variable size buffer ADT.
  9.  *   Copyright (C) 1996  Rene W.J. Pijlman
  10.  *
  11.  *   This program is free software; you can redistribute it and/or modify
  12.  *   it under the terms of the GNU General Public License as published by
  13.  *   the Free Software Foundation; either version 2 of the License, or
  14.  *   (at your option) any later version.
  15.  *
  16.  *   This program is distributed in the hope that it will be useful,
  17.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  *   GNU General Public License for more details.
  20.  *
  21.  *   You should have received a copy of the GNU General Public License
  22.  *   along with this program; if not, write to the Free Software
  23.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  * VERSION
  25.  *   $Header: /home/rene/sys/CVS_MasterSourceRepository/skim/VarBufTest.c,v 1.7 1996/02/18 11:40:25 rene Exp $
  26.  *   Distributed with Skim version 0.8.4.
  27.  */
  28.  
  29. #include "VarBuf.h"
  30. #include "StandardIO.h"
  31.  
  32. #include <assert.h>
  33. #include <string.h>
  34.  
  35. /* assert()'s must be in effect in this file. */
  36. #ifdef NDEBUG
  37. #    undef NDEBUG
  38. #endif
  39.  
  40. static void TestCreation( VarBuf * Buf1, VarBuf * Buf2 )
  41. {
  42.     VarBuf Temp = VBCreateString("ABC");
  43.  
  44.     VBDestroy( VBCreate() );
  45.  
  46.     *Buf1 = VBCreate();
  47.     *Buf2 = VBCreate();
  48.  
  49.     assert( !strcmp( VBAsString(Temp), "ABC" ) );
  50.     assert( VBSize(*Buf1) == 0 );
  51.     assert( VBSize(*Buf2) == 0 );
  52.     assert( strlen( VBAsString(*Buf1) ) == 0 );
  53.     assert( strlen( VBAsString(*Buf2) ) == 0 );
  54.  
  55.     VBDestroy( Temp );
  56. }
  57.  
  58.  
  59. static void TestAppendStringAndChar( VarBuf Buf1, VarBuf Buf2 )
  60. {
  61.     VBReset( Buf1 );
  62.     VBReset( Buf2 );
  63.  
  64.     VBAppendString( Buf1, "Alle" );
  65.     VBAppendCharacter( Buf1, ' ' );
  66.     VBAppendString( Buf2, "eendjes" );
  67.     VBAppendVB( Buf1, Buf2 );
  68.     VBAppendCharacter( Buf1, ' ' );
  69.     VBAppendString( Buf1, "zwemmen" );
  70.     VBAppendCharacter( Buf1, ' ' );
  71.     VBReset( Buf2 );
  72.     VBAppendString( Buf2, "in" );
  73.     VBAppendCharacter( Buf2, ' ' );
  74.     VBAppendString( Buf2, "het" );
  75.     VBAppendVB( Buf1, Buf2 );
  76.     VBAppendCharacter( Buf1, ' ' );
  77.     VBAppendString( Buf1, "water" );
  78.  
  79.     assert( !strcmp( VBAsString(Buf1), "Alle eendjes zwemmen in het water" ) );
  80. }
  81.  
  82.  
  83. static void TestTruncate( VarBuf Buf1, VarBuf Buf2 )
  84. {
  85.     VBReset( Buf1 );
  86.     VBAppendString( Buf1, "ABC" );
  87.     VBTruncate( Buf1, 1 );
  88.     assert( VBSize(Buf1) == 1 );
  89.     assert( !strcmp(VBAsString(Buf1), "A"));
  90.  
  91.     VBReset( Buf1 );
  92.     VBAppendString( Buf1, "ABC" );
  93.     VBTruncate( Buf1, 0 );
  94.     assert( VBSize(Buf1) == 0 );
  95.     assert( !strcmp(VBAsString(Buf1), ""));
  96.  
  97.     VBReset( Buf2 );
  98.     VBAppendString( Buf2, "ABC" );
  99.     VBTruncate( Buf2, 3 );
  100.     assert( VBSize(Buf2) == 3 );
  101.     assert( !strcmp(VBAsString(Buf2), "ABC"));
  102. }
  103.  
  104.  
  105. #define HUGE 100000
  106.  
  107. static void TestManyAppends( VarBuf Buf1, VarBuf Buf2 )
  108. {
  109.     int i;
  110.     VBReset( Buf1 );
  111.  
  112.     for ( i = 0; i < HUGE; i++ )
  113.     {
  114.         VBAppendCharacter( Buf1, 'x' );
  115.     }
  116.     assert( VBSize( Buf1 ) == HUGE );
  117.     assert( strlen( VBAsString( Buf1 ) ) == HUGE );
  118.  
  119.     for ( i = 0; i < HUGE; i++ )
  120.     {
  121.         assert( VBAsString(Buf1)[i] == 'x' );
  122.     }
  123. }
  124.  
  125.  
  126. static void TestShiftLeft( VarBuf Buf1, VarBuf Buf2 )
  127. {
  128.     VBReset( Buf1 );
  129.     VBAppendString( Buf1, "ABC" );
  130.     VBShiftLeft(Buf1, 2);
  131.     assert( VBSize(Buf1) == 1 );
  132.     assert( !strcmp(VBAsString(Buf1), "C"));
  133.  
  134.     VBReset( Buf1 );
  135.     VBAppendString( Buf1, "ABC" );
  136.     VBShiftLeft(Buf1, 3);
  137.     assert( VBSize(Buf1) == 0 );
  138.     assert( !strcmp(VBAsString(Buf1), ""));
  139.  
  140.     VBReset( Buf2 );
  141.     VBAppendString( Buf2, "ABC" );
  142.     VBShiftLeft(Buf2, 0);
  143.     assert( VBSize(Buf2) == 3 );
  144.     assert( !strcmp(VBAsString(Buf2), "ABC"));
  145. }
  146.  
  147. static void TestShiftRight( VarBuf Buf1, VarBuf Buf2 )
  148. {
  149.     VBReset( Buf1 );
  150.     VBAppendString( Buf1, "ABC" );
  151.     VBShiftRight(Buf1, 2, '>' );
  152.     assert( VBSize(Buf1) == 5 );
  153.     assert( !strcmp(VBAsString(Buf1), ">>ABC"));
  154.  
  155.     VBReset( Buf1 );
  156.     VBShiftRight(Buf1, 3, '>' );
  157.     assert( VBSize(Buf1) == 3 );
  158.     assert( !strcmp(VBAsString(Buf1), ">>>"));
  159.  
  160.     VBReset( Buf2 );
  161.     VBAppendString( Buf2, "ABC" );
  162.     VBShiftRight(Buf2, 0, '>' );
  163.     assert( VBSize(Buf2) == 3 );
  164.     assert( !strcmp(VBAsString(Buf2), "ABC"));
  165. }
  166.  
  167. #define TEMPFILENAME "/tmp/VarBufTest.tmp"
  168.  
  169. static void TestReadLine( VarBuf Buf1, VarBuf Buf2 )
  170. {
  171.     StandardIO Null = SIOCreate();
  172.     StandardIO Temp = SIOCreate();
  173.  
  174.     VBReset( Buf2 );
  175.  
  176.     SIOFileOpen( Null, "/dev/null", OpenModeRead );
  177.  
  178.     assert( Null != NULL );
  179.     assert( !VBReadLine( Buf2, Null, True ) );
  180.     assert( VBSize(Buf2) == 0 );
  181.     SIOFileClose( Null );
  182.  
  183.     SIOFileOpen( Temp, TEMPFILENAME, OpenModeWriteDiscardOld );
  184.  
  185.     SIOPrintf( Temp, "\n" );
  186.     SIOFileClose( Temp );
  187.  
  188.     SIOFileOpen( Temp, TEMPFILENAME, OpenModeRead );
  189.     VBReset( Buf1 );
  190.     VBReadLine( Buf1, Temp, True );
  191.     assert( VBSize( Buf1 ) == 1 );
  192.     assert( !strcmp( VBAsString(Buf1), "\n" ) );
  193.     SIOFileClose( Temp );
  194.  
  195.     SIOFileOpen( Temp, TEMPFILENAME, OpenModeRead );
  196.     VBReset( Buf1 );
  197.     assert( !strcmp( VBAsString(Buf1), "" ) );
  198.     VBReadLine( Buf1, Temp, False );
  199.     assert( VBSize( Buf1 ) == 0 );
  200.     assert( !strcmp( VBAsString(Buf1), "" ) );
  201.     assert( VBAsString(Buf1) != NULL );
  202.     assert( !strcmp( VBAsString(Buf1), "" ) );
  203.     SIOFileClose( Temp );
  204.  
  205.     SIOFileOpen( Temp, TEMPFILENAME, OpenModeWriteDiscardOld );
  206.     SIOPrintf(Temp, "%s", "alt.arts.nomad 0000000757 0000000554 y\n");
  207.     SIOPrintf(Temp, "%s", "alt.ascii-art 0000023551 21588 y\n" );
  208.     SIOPrintf(Temp, "%s", "alt.ascii-art.animation 0000001209 01109 y\n" );
  209.     SIOPrintf(Temp, "%s", "alt.asian-movies 0000016485 0000013889 y\n" );
  210.     SIOFileClose( Temp );
  211.  
  212.     SIOFileOpen( Temp, TEMPFILENAME, OpenModeRead );
  213.     VBReset( Buf1 );
  214.     VBReadLine( Buf1, Temp, True );
  215.     assert( !strcmp(VBAsString(Buf1), "alt.arts.nomad 0000000757 0000000554 y\n" ) );
  216.     VBReset( Buf1 );
  217.     VBReadLine( Buf1, Temp, True );
  218.     assert( !strcmp(VBAsString(Buf1), "alt.ascii-art 0000023551 21588 y\n" ));
  219.     VBReset( Buf1 );
  220.     VBReadLine( Buf1, Temp, True );
  221.     assert( !strcmp(VBAsString(Buf1), "alt.ascii-art.animation 0000001209 01109 y\n"));
  222.     VBReset( Buf1 );
  223.     VBReadLine( Buf1, Temp, True );
  224.     assert( !strcmp(VBAsString(Buf1), "alt.asian-movies 0000016485 0000013889 y\n"));
  225.     SIOFileClose( Temp );
  226.  
  227.     VBReset( Buf1 );
  228.     VBPrintf( Buf1, "A%cle %s %s in %she%c %s", 'l', "eendjes", "zwemmen", "",
  229.                     't', "water" );
  230.     assert( !strcmp( VBAsString(Buf1), "Alle eendjes zwemmen in het water" ) );
  231.  
  232.     VBReset( Buf1 );
  233.     VBReset( Buf2 );
  234.     VBPrintf( Buf2, "zwemmen in" );
  235.     VBPrintf( Buf1, "Alle %s %V het %s", "eendjes", Buf2, "water" );
  236.     assert( !strcmp( VBAsString(Buf1), "Alle eendjes zwemmen in het water" ) );
  237.  
  238.     system( "rm -f /tmp/VarBufTest.tmp" );
  239.  
  240.     SIODestroy( Null );
  241.     SIODestroy( Temp );
  242. }
  243.  
  244.  
  245. static void TestVBFixSize( VarBuf Buf1, VarBuf Buf2 )
  246. {
  247.     VBReset( Buf1 );
  248.     VBReset( Buf2 );
  249.  
  250.     VBFixSize( Buf1, 6, 'X' );
  251.     assert( !strcmp( VBAsString( Buf1 ), "XXXXXX" ) );
  252.  
  253.     VBFixSize( Buf1, 2, 'Y' );
  254.     assert( !strcmp( VBAsString( Buf1 ), "XX" ) );
  255.  
  256.     VBFixSize( Buf1, 4, 'Z' );
  257.     assert( !strcmp( VBAsString( Buf1 ), "XXZZ" ) );
  258.  
  259.     VBFixSize( Buf1, 0, 'Z' );
  260.     assert( VBSize( Buf1 ) == 0 );
  261.  
  262.     VBFixSize( Buf1, 10, 'X' );
  263.     VBFixSize( Buf1, 10, 'X' );
  264.     assert( !strcmp( VBAsString( Buf1 ), "XXXXXXXXXX" ) );
  265. }
  266.  
  267.  
  268. static void TestVBRemoveLeadingBlanks( VarBuf Buf1, VarBuf Buf2 )
  269. {
  270.     VBReset( Buf1 );
  271.     VBReset( Buf2 );
  272.  
  273.     VBRemoveLeadingBlanks( Buf1 );
  274.     assert( VBSize( Buf1 ) == 0 );
  275.  
  276.     VBAppendString( Buf1, " \t\n\f\r\v" );
  277.     VBRemoveLeadingBlanks( Buf1 );
  278.     assert( VBSize( Buf1 ) == 0 );
  279.  
  280.     VBAppendString( Buf1, "  XYZ  " );
  281.     VBRemoveLeadingBlanks( Buf1 );
  282.     assert( !strcmp( VBAsString( Buf1 ), "XYZ  " ) );
  283. }
  284.  
  285.  
  286. static void TestVBRemoveTrailingBlanks( VarBuf Buf1, VarBuf Buf2 )
  287. {
  288.     VBReset( Buf1 );
  289.     VBReset( Buf2 );
  290.  
  291.     VBRemoveTrailingBlanks( Buf1 );
  292.     assert( VBSize( Buf1 ) == 0 );
  293.  
  294.     VBAppendString( Buf1, " \t\n\f\r" );
  295.     VBRemoveTrailingBlanks( Buf1 );
  296.     assert( VBSize( Buf1 ) == 0 );
  297.  
  298.     VBAppendString( Buf1, "  XYZ  " );
  299.     VBRemoveTrailingBlanks( Buf1 );
  300.     assert( !strcmp( VBAsString(Buf1), "  XYZ" ) );
  301. }
  302.  
  303.  
  304.  
  305. static void TestSplit( VarBuf Buf1, VarBuf Buf2 )
  306. {
  307.     VarBuf Buf3 = VBCreate();
  308.     const char * SplitString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  309.     int i;
  310.  
  311.     VBAppendString( Buf3, SplitString );
  312.  
  313.     for ( i = 0; i < strlen(SplitString); i++ )
  314.     {
  315.     VBReset( Buf1 );
  316.     VBReset( Buf2 );
  317.  
  318.     VBSplitAtOffset( Buf3, i, Buf1, Buf2 );
  319.  
  320.     assert( !strcmp( VBAsString(Buf3), SplitString ) );
  321.  
  322.     assert( VBSize(Buf1) == i );
  323.     assert( !strncmp( VBAsString(Buf1), SplitString, i ) );
  324.  
  325.     assert( VBSize(Buf2) == ( strlen(SplitString) - i ) );
  326.     assert( !strcmp( VBAsString(Buf2), SplitString + i ) );
  327.     }
  328.  
  329.     VBDestroy( Buf3 );
  330. }
  331.  
  332.  
  333. static int MyCompare( VarBuf Buf1, VarBuf Buf2 )
  334. {
  335.     return strcmp( VBAsString(Buf1), VBAsString(Buf2) );
  336. }
  337.  
  338.  
  339. static void TestSort( VarBuf Buf1, VarBuf Buf2 )
  340. {
  341.     VarBuf * p;
  342.  
  343.     VarBuf A = VBCreate();
  344.     VarBuf B = VBCreate();
  345.     VarBuf C = VBCreate();
  346.     VarBuf D = VBCreate();
  347.     VarBuf E = VBCreate();
  348.     VarBuf F = VBCreate();
  349.  
  350.     VarBuf Array = VBCreate();
  351.  
  352.     VBAppendString( A, "ABCDE" );
  353.     VBAppendString( B, "BCDEF" );
  354.     VBAppendString( C, "CDEFG" );
  355.     VBAppendString( D, "DEFGH" );
  356.     VBAppendString( E, "EFGHI" );
  357.     VBAppendString( F, "JKLMN" );
  358.  
  359.     VBAppendVBReference( Array, D );
  360.     VBAppendVBReference( Array, A );
  361.     VBAppendVBReference( Array, B );
  362.     VBAppendVBReference( Array, F );
  363.     VBAppendVBReference( Array, E );
  364.     VBAppendVBReference( Array, C );
  365.  
  366.     VBSortArrayOfVB( Array, MyCompare );
  367.  
  368.     p = (VarBuf *)VBAddress(Array);
  369.  
  370.     assert( p[0] == A );
  371.     assert( p[1] == B );
  372.     assert( p[2] == C );
  373.     assert( p[3] == D );
  374.     assert( p[4] == E );
  375.     assert( p[5] == F );
  376.  
  377.     VBDestroy(A);
  378.     VBDestroy(B);
  379.     VBDestroy(C);
  380.     VBDestroy(D);
  381.     VBDestroy(E);
  382.     VBDestroy(F);
  383.  
  384.     VBDestroy(Array);
  385. }
  386.  
  387.  
  388. int main( void )
  389. {
  390.     VarBuf Buf1;
  391.     VarBuf Buf2;
  392.  
  393.     TestCreation( &Buf1, &Buf2 );
  394.  
  395.     TestSort( Buf1, Buf2 );
  396.  
  397.     TestSplit( Buf1, Buf2 );
  398.  
  399.     TestAppendStringAndChar( Buf1, Buf2 );
  400.  
  401.     TestManyAppends( Buf1, Buf2 );
  402.  
  403.     TestTruncate( Buf1, Buf2 );
  404.  
  405.     TestReadLine( Buf1, Buf2 );
  406.  
  407.     TestShiftLeft( Buf1, Buf2 );
  408.  
  409.     TestShiftRight( Buf1, Buf2 );
  410.  
  411.     TestVBFixSize( Buf1, Buf2 );
  412.  
  413.     TestVBRemoveLeadingBlanks( Buf1, Buf2 );
  414.  
  415.     TestVBRemoveTrailingBlanks( Buf1, Buf2 );
  416.  
  417.     VBDestroy( Buf1 );
  418.     VBDestroy( Buf2 );
  419.  
  420.     SIOPrintf( StandardOutput, "OK\n" );
  421.  
  422.     return EXIT_SUCCESS;
  423. }
  424.